import 'package:flutter/material.dart';
import 'package:intl/intl.dart';

abstract class BaseRecordDialog extends StatefulWidget {
  final String babyId;

  const BaseRecordDialog({Key? key, required this.babyId}) : super(key: key);
}

abstract class BaseRecordDialogState<T extends BaseRecordDialog>
    extends State<T> {
  late DateTime selectedDateTime;
  String notes = '';

  @override
  void initState() {
    super.initState();
    selectedDateTime = DateTime.now();
  }

  Widget buildCustomContent(BuildContext context);
  Future<void> saveRecord(BuildContext context);

  @override
  Widget build(BuildContext context) {
    return AlertDialog(
      title: Text(getDialogTitle()),
      content: SingleChildScrollView(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            _buildDateTimePicker(context),
            SizedBox(height: 16),
            buildCustomContent(context),
            SizedBox(height: 16),
            TextField(
              decoration: InputDecoration(
                labelText: '备注',
                border: OutlineInputBorder(),
              ),
              maxLines: 3,
              onChanged: (value) => notes = value,
            ),
          ],
        ),
      ),
      actions: [
        TextButton(onPressed: () => Navigator.pop(context), child: Text('取消')),
        TextButton(onPressed: () => saveRecord(context), child: Text('保存')),
      ],
    );
  }

  Widget _buildDateTimePicker(BuildContext context) {
    return Row(
      children: [
        Text('时间: '),
        TextButton(
          child: Text(DateFormat('yyyy-MM-dd HH:mm').format(selectedDateTime)),
          onPressed: () async {
            final date = await showDatePicker(
              context: context,
              initialDate: selectedDateTime,
              firstDate: DateTime(2020),
              lastDate: DateTime.now(),
            );
            if (date != null) {
              final time = await showTimePicker(
                context: context,
                initialTime: TimeOfDay.fromDateTime(selectedDateTime),
              );
              if (time != null) {
                setState(() {
                  selectedDateTime = DateTime(
                    date.year,
                    date.month,
                    date.day,
                    time.hour,
                    time.minute,
                  );
                });
              }
            }
          },
        ),
      ],
    );
  }

  String getDialogTitle();
}
